package com.neuedu.api.collection;

import java.util.*;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述  迭代器
 * @data 2024/10/2514:12
 */
public class MyIterator {

    public static void main(String[] args) {

        /**
         * 构造集合 .UnsupportedOperationException
         */
        List l1 = Arrays.asList(0,1,2,3,4,5,6,7);

        //LinkedList 包装下
        LinkedList linkedList =  new LinkedList(l1);
        linkedList.add(100);


        //迭代器
        for (Iterator iterator = linkedList.iterator();iterator.hasNext(); ) {
            Object item = iterator.next();
            System.out.println("item = " + item);
        }

        // [   0,1,2,3,  4***, 5,6,7  ,100  ]
        ListIterator listIterator = linkedList.listIterator(4);

        Object next = listIterator.next();
        System.out.println("next = " + next);  //打印 4  指向 5
        Object previous = listIterator.previous();//当前  5  指向4
        System.out.println("previous = " + previous);  //打印 4  指向 5

        previous = listIterator.previous();
        System.out.println("previous = " + previous);  //3

        previous = listIterator.previous();
        System.out.println("previous = " + previous);  //2

        previous = listIterator.previous();
        System.out.println("previous = " + previous);  //1

        previous = listIterator.previous();
        System.out.println("previous = " + previous);  // 0

        previous = listIterator.previous();//当前  5  指向4
        System.out.println("previous = " + previous);  //打印 4  指向 5

    }



}
